package com.xique.system.controller;

import com.xique.common.core.constant.OssFilePrefixConstants;
import com.xique.common.core.utils.AliossUtil;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.system.bean.domain.LoginPage;
import com.xique.system.bean.domain.SystemUser;
import com.xique.system.bean.search.SystemUserSearch;
import com.xique.system.service.service.ILoginPageService;
import com.xique.system.service.service.ISystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 登录页Controller
 *
 * @author caogq
 * @date 2022-03-09
 */
@RestController
@RequestMapping("/loginPage")
public class LoginPageController extends BaseController {
    @Autowired
    private ILoginPageService loginPageService;

    @Autowired
    private AliossUtil aliossUtil;

    @Autowired
    private ISystemUserService systemUserService;


    /**
     * 查询登录页列表
     */
    @PreAuthorize(hasPermi = "oms:loginPage:list")
    @GetMapping("/list")
    public TableDataInfo list(LoginPage loginPage) {
        startPage();
        List<LoginPage> list = loginPageService.selectLoginPageList(loginPage);
        List<Long> createIds = list.stream().map(e -> Long.valueOf(e.getCreateBy())).collect(Collectors.toList());
        SystemUserSearch systemUserSearch = new SystemUserSearch();
        systemUserSearch.setUserIds(createIds);
        List<SystemUser> systemUsers = systemUserService.selectSystemUserListBySearch(systemUserSearch);
        for (LoginPage page : list) {
            if (!systemUsers.isEmpty()) {
                SystemUser systemUser = systemUsers.stream().filter(e -> String.valueOf(e.getUserId()).equals(page.getCreateBy())).findAny().orElse(null);
                page.setCreateName(systemUser != null ? systemUser.getUserName() : "");
            }
            if (StringUtils.isEmpty(page.getStatus()) || !"4".equals(page.getStatus())) {
                page.setShowStatus(loginPageService.selectLoginPageStatus(page.getStartTime(), page.getEndTime()));
            } else {
                page.setShowStatus(page.getStatus());
            }
        }
        return getDataTable(list);
    }

    /**
     * 新增保存登录页
     */
    @PreAuthorize(hasPermi = "oms:loginPage:add")
    @Log(title = "登录页", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSave(LoginPage loginPage) {
        Map<String, Object> upload = aliossUtil.uploadFile(loginPage.getFile(), OssFilePrefixConstants.LOGIN_PAGE);
        String url = (String) upload.get("url");
        loginPage.setPicUrl(url);
//        if (loginPage.getStartTime().before(DateUtils.getNowDate())) {
//            loginPage.setStatus("2");
//        } else {
//            loginPage.setStatus("1");
//        }
        //默认启用
        loginPage.setStatus("Y");
        loginPage.setStartTime(DateUtils.getStartOfDay(loginPage.getStartTime()));
        loginPage.setEndTime(DateUtils.getEndOfDay(loginPage.getEndTime()));
        return toAjax(loginPageService.insertLoginPage(loginPage));
    }


    /**
     * 修改保存登录页
     */
    @PreAuthorize(hasPermi = "oms:loginPage:edit")
    @Log(title = "登录页", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult editSave(LoginPage request) {
        if (request.getNewFile() != null) {
            Map<String, Object> upload = aliossUtil.uploadFile(request.getNewFile(), OssFilePrefixConstants.LOGIN_PAGE);
            String url = (String) upload.get("url");
            request.setPicUrl(url);
        }
        return toAjax(loginPageService.updateLoginPage(request));
    }

    /**
     * 删除登录页
     */
    @PreAuthorize(hasPermi = "oms:loginPage:remove")
    @Log(title = "登录页", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(String ids) {
        return toAjax(loginPageService.deleteLoginPageByIds(ids));
    }


    /**
     * 查询系统登录页
     */
    @GetMapping("/currentList")
    public AjaxResult currentList(LoginPage loginPage) {
        List<LoginPage> list = loginPageService.selectCurrentList(loginPage);
        return AjaxResult.success(list);
    }

    /**
     * 手动上下架
     */
    @PostMapping("/changeStatus")
    public AjaxResult online(@RequestBody LoginPage loginPage) {
        return toAjax(loginPageService.updateLoginPage(loginPage));
    }

    /**
     * 自动上下架
     */
    @GetMapping("/autoOnOffLoginPage")
    public AjaxResult autoOnOffLoginPage() {
        return loginPageService.autoOnOffLoginPage();
    }
}
